package com.avricot.userinfo.os;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.avricot.userinfo.UserInfoUtils;

public enum OSName {
    WINDOWS {
	@Override
	public String getVersion(final String userAgent) {
	    if (userAgent.contains("Windows ME")) {
		return null;
	    }
	    final String version = UserInfoUtils.getVersionAfterIdentifier(userAgent, "Windows ");
	    if (version == null) {
		return UserInfoUtils.getVersionAfterIdentifier(userAgent, "WinNT");
	    }
	    if (version.equals("NT 5.0")) {
		return "2000";
	    }
	    if (version.equals("NT 5.1")) {
		return "XP";
	    }
	    if (version.equals("NT 5.2")) {
		return "Server 2003";
	    }
	    if (version.equals("NT 6.0")) {
		return "Vista";
	    }
	    if (version.equals("NT 6.1")) {
		return "7";
	    }
	    return version;
	}
    },
    MAC {
	@Override
	public String getVersion(final String userAgent) {
	    String version = UserInfoUtils.getVersionAfterIdentifier(userAgent, "Mac OS");
	    version = version != null ? version : UserInfoUtils.getVersionAfterIdentifier(userAgent, "Macintosh");
	    return version != null ? version : UserInfoUtils.getVersionAfterIdentifier(userAgent, "Mac_PowerPC");
	}
    },
    LINUX {
	@Override
	public String getVersion(final String userAgent) {
	    final String version = UserInfoUtils.getVersionAfterIdentifier(userAgent, "Linux");
	    return version != null ? version : UserInfoUtils.getVersionAfterIdentifier(userAgent, "X11");
	}
    },
    ANDROID {
	@Override
	public boolean isMobileDevice() {
	    return true;
	}

	@Override
	public String getVersion(final String userAgent) {
	    return UserInfoUtils.getVersionAfterIdentifier(userAgent, "Android ");
	}

    },
    WINDOWS_ME {
	@Override
	public boolean isMobileDevice() {
	    return true;
	}

	@Override
	public String getVersion(final String userAgent) {
	    return UserInfoUtils.getVersionAfterIdentifier(userAgent, "Windows ME");
	}
    },
    WGET {
	@Override
	public String getVersion(final String userAgent) {
	    return UserInfoUtils.getVersionAfterIdentifier(userAgent, "wget");
	}
    },
    W3C_VALIDATOR {
	@Override
	public String getVersion(final String userAgent) {
	    return UserInfoUtils.getVersionAfterIdentifier(userAgent, "W3C_Validator");
	}
    },
    BOT {
	@Override
	public String getVersion(final String userAgent) {
	    final Pattern p = Pattern.compile(UserInfoUtils.botUserAgentRegex);
	    final Matcher m = p.matcher(userAgent);
	    return m.find() ? "" : null;
	}
    },
    OPEN_BSD {
	@Override
	public String getVersion(final String userAgent) {
	    return UserInfoUtils.getVersionAfterIdentifier(userAgent, "OpenBSD");
	}
    },
    QNX {
	@Override
	public String getVersion(final String userAgent) {
	    return UserInfoUtils.getVersionAfterIdentifier(userAgent, "QNX");
	}
    },
    BEOS {
	@Override
	public String getVersion(final String userAgent) {
	    return UserInfoUtils.getVersionAfterIdentifier(userAgent, "BeOS ");
	}
    },
    UNKOWN() {
	@Override
	public String getVersion(final String userAgent) {
	    return "";
	}
    };

    public boolean isMobileDevice() {
	return false;
    }

    public abstract String getVersion(String userAgent);
}
